﻿#include "UserWorker.h"
#include <QJsonDocument>
#include <SqliteDatabase.h>

UserWorker::UserWorker(QString databaseType, QString databaseName, QObject *parent) : QObject(parent)
  ,m_tableName("user")
  ,m_databaseType(databaseType)
  ,m_databaseName(databaseName)
{

}
UserWorker::~UserWorker()
{
    if(database!=nullptr){
        database->deleteLater();
        database = nullptr;
    }
}

void UserWorker::onInit()
{
    database = new SqliteDatabase(NULL,m_databaseName,m_tableName, m_databaseType);
    createRoot();
    createSuperAdmin();
    createAdmin();
    createBaseUser();
}
std::string textEncry(const char* strI)
{
    //字符串加密
    std::string strReturn = strI;
    int len = strlen(strReturn.c_str());
    for (int i = 0; i < len; i++)
    {
        strReturn[i] = strReturn[i] + i + 1;
    }
    return strReturn;
}
void UserWorker::createRoot()
{
    //ImperialVision

    QString rootID = QString::fromStdString(textEncry("root"));
    QJsonObject userData;
    userData[WORKERNAME] = rootID;
    userData[WORKERPASSWORD] = rootID;
    userData[WORKERLEVEL] = WL_ROOT;
    createUser(rootID, userData);
}
void UserWorker::createSuperAdmin()
{
    //MinTai
    QString superadminID = QString::fromStdString(textEncry("superadmin"));
    QJsonObject userData;
    userData[WORKERNAME] = superadminID;
    userData[WORKERPASSWORD] = superadminID;
    userData[WORKERLEVEL] = WL_SUPERADMIN;
    createUser(superadminID, userData);
}
void UserWorker::createAdmin()
{
    //admin
    QString adminID = QString::fromStdString(textEncry("admin"));
    QJsonObject userData;
    userData[WORKERNAME] = adminID;
    userData[WORKERPASSWORD] = adminID;
    userData[WORKERLEVEL] = WL_ADMIN;
    createUser(adminID, userData);
}
void UserWorker::createBaseUser()
{
    //user
    QString userID = QString::fromStdString(textEncry("user"));
    QJsonObject userData;
    userData[WORKERNAME] = userID;
    userData[WORKERPASSWORD] = userID;
    userData[WORKERLEVEL] = WL_OPERATOR;
    createUser(userID, userData);
}

QJsonArray UserWorker::readAllUser()
{
    auto arr = database->readAllRows(m_tableName);
    return arr;
}

QJsonObject UserWorker::find(const QString &workID)
{
    QJsonObject jsonData;
    auto r = database->find(m_tableName,QString("\"%1\":\"%2\"").arg(WORKERID,workID));
    if(!r.isEmpty())
        jsonData =  r.at(0).toObject();
    return jsonData;
}

QJsonObject UserWorker::readUser(const QString& workID)
{
    auto record = find(workID);
    if(record.isEmpty()){
        UserEntity entity;
        entity.mWorkID = workID;
        database->createRow(m_tableName,entity.mId,QJsonDocument::fromVariant(entity.toVariant()).object());
        record = find(workID);
    }
    return record["data"].toObject();
}

bool UserWorker::updateUser(const QString& workID,const QJsonObject &data)
{
    auto record = find(workID);
    if(record.isEmpty())
        return false;

    record.insert("data",data);
    return database->updateRow(m_tableName,record["id"].toString(),record);
}

bool UserWorker::deleteUser(const QString &workID)
{
    auto record = find(workID);
    if(record.isEmpty()){
        return false;
    }
    return database->deleteRow(m_tableName,record["id"].toString());
}

bool UserWorker::createUser(const QString &workID, const QJsonObject &data)
{
    auto record = find(workID);
    if(!record.isEmpty())
        return false;
    UserEntity entity;
    entity.mWorkID = workID;
    entity.mData = data;
    return database->createRow(m_tableName,entity.mId,QJsonDocument::fromVariant(entity.toVariant()).object());
}
void UserWorker::onOperate(UserOptID optID, QJsonObject optObj)
{
    switch (optID) {
    case Opt_Login:
    {
        QJsonObject readObj = readUser(optObj.value(WORKERID).toString());
        if(optObj.value(WORKERPASSWORD).toString() == readObj.value(WORKERPASSWORD).toString()){
            readObj.insert(WORKERID,optObj.value(WORKERID).toString());
            emit sglOperateResult(optID,true,readObj);
            emit sglAllUser(readAllUser());
        }else{
            emit sglOperateResult(optID,false,QJsonObject());
        }
    }
        break;
    case Opt_Register:
    {
        QString workID = optObj.value(WORKERID).toString();
        QJsonObject userData;
        userData[WORKERNAME] = optObj.value(WORKERNAME).toString();
        userData[WORKERPASSWORD] = optObj.value(WORKERPASSWORD).toString();
        userData[WORKERLEVEL] = optObj.value(WORKERLEVEL).toInt();
        if(createUser(workID, userData)){
            emit sglOperateResult(optID,true,QJsonObject());
            emit sglAllUser(readAllUser());
        }else{
            emit sglOperateResult(optID,false,QJsonObject());
        }
    }
        break;
    case Opt_ModifyPwd:
    {
        QJsonObject readObj = readUser(optObj.value(WORKERID).toString());
        QString workID = optObj.value(WORKERID).toString();
        QJsonObject userData;
        userData[WORKERNAME] = readObj.value(WORKERNAME).toString();
        userData[WORKERPASSWORD] = optObj.value(WORKERPASSWORD).toString();
        userData[WORKERLEVEL] = readObj.value(WORKERLEVEL).toInt();
        if(updateUser(workID, userData)){
            emit sglOperateResult(optID,true,userData);
        }else{
            emit sglOperateResult(optID,false,QJsonObject());
        }
    }
        break;
    case Opt_ModifyInfo:
    {
        QJsonObject readObj = readUser(optObj.value(WORKERID).toString());
        QString workID = optObj.value(WORKERID).toString();
        QJsonObject userData;
        userData[WORKERNAME] = optObj.value(WORKERNAME).toString();
        userData[WORKERPASSWORD] = readObj.value(WORKERPASSWORD).toString();
        userData[WORKERLEVEL] = optObj.value(WORKERLEVEL).toInt();
        if(updateUser(workID, userData)){
            emit sglOperateResult(optID,true,userData);
            emit sglAllUser(readAllUser());
        }else{
            emit sglOperateResult(optID,false,QJsonObject());
        }
    }
        break;
    case Opt_Delete:
    {
        if(deleteUser(optObj.value(WORKERID).toString())){
            emit sglOperateResult(optID,true,QJsonObject());
            emit sglAllUser(readAllUser());
        }else{
            emit sglOperateResult(optID,false,QJsonObject());
        }

    }
        break;
    default:
        break;
    }
}
